<?php

class UserCriteria extends Criteria
{

    /**
     * Creaza un criteriu de selectie restrictionata la user-ul curent.
     * Se foloseste in constructia new UserCriteria(__CLASS__)
     *
     * @param string $peerClass Denumirea clasei care modeleaza o tabela
     * @param array $month luna si anul - optional
     */
    public function __construct($peerClass, $month = array())
    {
        // check if $peerClass is a valid class
        if (!class_exists($peerClass))
        {
            throw new InvalidArgumentException('Class \''.$peerClass.'\' doesn\'t exist.');
        }

        parent::__construct();

        $this->add(constant($peerClass . '::' . Peer::USER_ID), self::getUserId());

        if ($month)
        {
            $this->add(constant($peerClass . '::' . Peer::LUNA), $month);
        }

        $nameColumn = @constant($peerClass . '::NAME');
        if (isset($nameColumn))
        {
            $this->addAscendingOrderByColumn($nameColumn);
        }
    }

    /**
     * Selecteaza coloane
     *
     * @param array $columns
     * @return UserCriteria
     *
     * Pot fi specificate si alias-uri: alias => SQL clause
     */
    public function addColumns(array $columns)
    {
        foreach ($columns as $key => $value)
        {
            if (is_numeric($key))
            {
                $this->addSelectColumn($value);
            }
            else
            {
                $this->addAsColumn($key, $value);
            }
        }

        return $this;
    }

    static public function getUserId()
    {
        return sfContext::getInstance()->getUser()->getId();
    }

}
